package com.yunshipei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.LabelListModel;

import java.util.List;

/**
 * Created by PC on 2016/6/20.
 */
public class TabListAdapter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private int mCurrentTabPosition;
    private List<LabelListModel> labelListModels;

    public TabListAdapter(Context context, List<LabelListModel> labelListModels, int currentTabPosition) {
        this.context = context;
        this.labelListModels = labelListModels;
        this.mCurrentTabPosition = currentTabPosition;
    }

    @Override
    public int getCount() {
        return labelListModels == null ? 0 : labelListModels.size();
    }

    @Override
    public LabelListModel getItem(int position) {
        return labelListModels.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.item_fragment_browser_tab, null, false);
            holder = new ViewHolder();
            holder.close = (ImageView) convertView.findViewById(R.id.close);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.thumb = (ImageView) convertView.findViewById(R.id.thumb);
            holder.close.setOnClickListener(listener);
            holder.title.setOnClickListener(listener);
            holder.thumb.setOnClickListener(listener);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == mCurrentTabPosition) {
            holder.close.setBackgroundColor(EnterConfig.getInstance().getGlobalColor());
        } else {
            holder.close.setBackgroundColor(context.getResources().getColor(R.color.black));
        }
        holder.close.setTag(position);
        holder.thumb.setTag(position);
        holder.title.setTag(position);
        LabelListModel labelListModel = getItem(position);
        holder.title.setText(labelListModel.getTitle());
        Glide.with(context).load(labelListModel.getImageUrl())
                .centerCrop()
                .dontAnimate()
                .into(holder.thumb);
        if (position == 0) {
            holder.close.setImageResource(R.drawable.tab_home_icon);
        } else {
            holder.close.setImageResource(R.drawable.right_tab_close);
        }
        return convertView;
    }

    public void setItemClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    public void notifyDataSetChanged(int currentTabPosition) {
        this.mCurrentTabPosition = currentTabPosition;
        super.notifyDataSetChanged();
    }

    private static class ViewHolder {
        ImageView close;
        TextView title;
        ImageView thumb;
    }

}